package com.hustbaidu.hustmap;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class PlaceInfoActivity extends Activity {
	private ImageView placeImgWidget;
	private TextView placeNameWidget;
	private TextView placeTypeWidget;
	private TextView placeRoadWidget;
	
	private ImageButton pathSearchToWidget;
	private ImageButton pathSearchFromWidget;
	private ImageButton showOnMapWidget;
	
	private TextView placeIntroduceWidget;
	
	private ImageButton lectureGoWidget;
	private TextView lectureNumWidget;

	private ImageButton commentGoWidget;
	private TextView commentNumWidget;
	
	private ImageButton scoreGoWidget;
	private TextView scoreNumWidget;
	private TextView averageScoreWidget;
	private TextView scoreSummaryWidget;
	
	private PlaceInfo place;
	private PlaceType type;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.place_info);
		
		Bundle bundle = getIntent().getExtras();
		place = bundle.getParcelable("place");
		type = bundle.getParcelable("type");
		
		placeImgWidget = (ImageView)findViewById(R.id.place_img);
		placeImgWidget.setImageBitmap(getLoacalBitmap(place.getPlaceImg()));
		
		placeNameWidget = (TextView)findViewById(R.id.place_name);
		placeNameWidget.setText(place.getPlaceName());
		
		placeTypeWidget = (TextView)findViewById(R.id.place_type);
		placeTypeWidget.setText(type.getTypeName());
		
		placeRoadWidget = (TextView)findViewById(R.id.place_road);
//		placeRoadWidget.setText(place.getPlaceRoad());
		
		pathSearchToWidget = (ImageButton)findViewById(R.id.place_path_search_to);
		pathSearchToWidget.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LayoutInflater li = LayoutInflater.from(PlaceInfoActivity.this);
				View view = li.inflate(R.layout.search_path_prompt_dialog, null);
				AlertDialog.Builder builder = new AlertDialog.Builder(PlaceInfoActivity.this);
				builder.setTitle("搜索地点");
				builder.setView(view);
		
				SearchPathPromptListener spPL = new SearchPathPromptListener(view);
				builder.setPositiveButton("确定", spPL);
				builder.setNegativeButton("取消", spPL);
				
				AlertDialog ad = builder.create();
				spPL.setPromptStopPlace(place.getPlaceName());
				ad.show();
			}
		});
		
		placeIntroduceWidget = (TextView)findViewById(R.id.place_introduce);
		placeIntroduceWidget.setText(place.getPlaceIntroduce());
		
		lectureNumWidget = (TextView)findViewById(R.id.lecture_number);
		lectureNumWidget.setText("(共" + LectureManager.getLectureNumberByPlace(place.getId()) + "个)");
		
		lectureGoWidget = (ImageButton)findViewById(R.id.lecture_go_image_button);
		lectureGoWidget.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putParcelable("place", place);
				intent.putExtras(bundle);
				intent.setClass(PlaceInfoActivity.this, LectureListActivity.class);
				startActivity(intent);
			}
			
		});
		
		commentNumWidget = (TextView)findViewById(R.id.comment_number);
		commentNumWidget.setText("(共" + CommentManager.getCommentNumberByPlace(place.getId()) + "条)");
				
		commentGoWidget = (ImageButton)findViewById(R.id.comment_go_image_button);
		commentGoWidget.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putParcelable("place", place);
				intent.putExtras(bundle);
				intent.setClass(PlaceInfoActivity.this, CommentListActivity.class);
				startActivity(intent);
			}
		});
		
		scoreNumWidget = (TextView)findViewById(R.id.score_number);
		scoreNumWidget.setText("(共" + ScoreManager.getScoreNumberByPlace(place.getId()) + "条)");
		
		averageScoreWidget = (TextView)findViewById(R.id.average_score);
		averageScoreWidget.setText(ScoreManager.getAverageScoreByPlace(place.getId()) + "分");
		
		
		
		scoreGoWidget = (ImageButton)findViewById(R.id.score_go_image_button);
		scoreGoWidget.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putParcelable("place", place);
				intent.putExtras(bundle);
				intent.setClass(PlaceInfoActivity.this, ScoreListActivity.class);
				startActivity(intent);
			}
		});
		
	}
	
	public static Bitmap getLoacalBitmap(String url) {
        try {
             FileInputStream fis = new FileInputStream(url);
             return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片         

          } catch (FileNotFoundException e) {
             e.printStackTrace();
             return null;
        }
   }

	private class SearchPathPromptListener implements android.content.DialogInterface.OnClickListener
	{
		// local variable to return the prompt reply value
		private String promptStartPlace = null;
		private String promptStopPlace = null;
		//Keep a variable for the view to retrieve the prompt value
		View promptDialogView = null;
		//Take in the view in the constructor
		public SearchPathPromptListener(View inDialogView) {
			promptDialogView = inDialogView;
		}
		
		//Call back method from dialogs
		public void onClick(DialogInterface v, int buttonId) {
			if (buttonId == DialogInterface.BUTTON1) {
				//ok button
				promptStartPlace = getStartPromptText();
				promptStopPlace = getStopPromptText();
//				hustMapView.searchPath(promptStartPlace, promptStopPlace);
				Intent intent = new Intent(PlaceInfoActivity.this, HustMapActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("start_place", promptStartPlace);
				bundle.putString("stop_place", promptStopPlace);
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				PlaceInfoActivity.this.finish();
			}
			else {
				//cancel button
//				promptValue = null;
			}
		}
		
		//Just an access method for what is in the edit box
		private String getStartPromptText() {
			EditText et = (EditText)promptDialogView.findViewById(R.id.start_place_prompt);
			return et.getText().toString();
		}
		
		private String getStopPromptText() {
			EditText et = (EditText)promptDialogView.findViewById(R.id.stop_place_prompt);
			return et.getText().toString();
		}

		public String getPromptStartPlace() {
			return promptStartPlace;
		}
		
		public void setPromptStartPlace(String promptStartPlace) {
			EditText et = (EditText)promptDialogView.findViewById(R.id.start_place_prompt);
			et.setText(promptStartPlace);
		}

		public String getPromptStopPlace() {
			return promptStopPlace;
		}
		
		public void setPromptStopPlace(String promptStopPlace) {
			EditText et = (EditText)promptDialogView.findViewById(R.id.stop_place_prompt);
			et.setText(promptStopPlace);
		}
		
	}

}
